﻿using System.Linq;

namespace Sidvall.Data
{
    public class SortItemCollection<TField> : System.Collections.ObjectModel.Collection<SortItem>
    {
        #region Business Members

        #region Add

        public void Add(TField fieldId)
        {
            Add(new SortItem(fieldId.ToString()));
        }
        public void Add(TField fieldId, bool ascending)
        {
            Add(new SortItem(fieldId.ToString(), ascending));
        }
        public void Add(TField fieldId, SortType sortType)
        {
            switch (sortType)
            {
                case SortType.Ascending:
                    Add(fieldId, true);
                    break;
                case SortType.Descending:
                    Add(fieldId, false);
                    break;
                default:
                    break;
            }
        }

        #endregion
        #region AddCustom

        public void AddCustom(string fieldName)
        {
            var item = new SortItem(fieldName);
            Add(item);
        }
        public void AddCustom(string fieldName, bool ascending)
        {
            var item = new SortItem(fieldName, ascending);
            Add(item);
        }
        public void AddCustom(string fieldName, SortType sortType)
        {
            switch (sortType)
            {
                case SortType.Ascending:
                    AddCustom(fieldName, true);
                    break;
                case SortType.Descending:
                    AddCustom(fieldName, false);
                    break;
                default:
                    break;
            }
        }


        #endregion
        #region AddRange

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public void AddRange(System.Collections.Generic.IEnumerable<SortItem> items)
        {
            if (items == null)
                return;
            foreach (var item in items)
            {
                if (item != null)
                    Add(item);
            }
        }

        #endregion
        #region ContainsField

        public bool ContainsField(TField fieldId)
        {
            return ContainsFieldName(fieldId.ToString());
        }

        #endregion
        #region ContainsFieldName

        public bool ContainsFieldName(string fieldName)
        {
            if (string.IsNullOrWhiteSpace(fieldName))
                return false;
            foreach (var item in this)
            {
                if (item.FieldName.Equals(fieldName, System.StringComparison.OrdinalIgnoreCase))
                    return true;
            }
            return false;
        }

        #endregion
        #region Copy

        public SortItemCollection<TField> Copy()
        {
            var items = new SortItemCollection<TField>();
            foreach (var item in this)
                items.Add(item.Copy());
            return items;
        }

        #endregion
        #region RemoveAllExcept

        public void RemoveAllExcept(System.Collections.Generic.IEnumerable<TField> fieldIds)
        {
            var list = (from o in fieldIds
                        select o.ToString());
            RemoveAllExcept(list);
        }
        public void RemoveAllExcept(System.Collections.Generic.IEnumerable<string> fieldNames)
        {
            if ((fieldNames != null) && (fieldNames.Any()))
            {
                var index = new System.Collections.Generic.HashSet<string>(fieldNames, System.StringComparer.OrdinalIgnoreCase);
                var iMax = Count;
                for (int i = iMax - 1; i >= 0; i--)
                {
                    var item = this[i];
                    if (!index.Contains(item.FieldName))
                        RemoveAt(i);
                }
            }
            else
            {
                Clear();
                return;
            }
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            var sb = new System.Text.StringBuilder();
            foreach (var item in this)
            {
                if (sb.Length > 0)
                    sb.Append(", ");
                sb.Append(item.ToString());
            }
            return sb.ToString();
        }

        #endregion

        #endregion
        #region Constructors

        public SortItemCollection()
        {
        }

        #endregion
    }
}
